#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "scenetable.h"
#include "scenemark.h"

#include <QBuffer>
#include <QMouseEvent>
#include <typeinfo>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QtXml/QDomDocument>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //QList<SceneMark> sts;
    this->st=new SceneTable(this);
    newsm=new SceneMark(st);
    newsm->setConfig();
    connect(st,SIGNAL(chvalue_changed(float)),newsm,SLOT(ValueChanged(float)));
    ui->gridLayout_2->addWidget(st);
    newpage();
    this->st->setConfig();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newpage()
{
    //this->st=NULL;
    //this->st=new SceneTable(this);
    //newsm=new SceneMark(st);
    //connect(st,SIGNAL(chvalue_changed(float)),newsm,SLOT(ValueChanged(float)));
    //ui->gridLayout_2->addWidget(st);
}


void MainWindow::on_actionSave_triggered()
{
    QByteArray tmp;
    QFileInfo fi;
    QString name;
    this->file_ProjectSave=QFileDialog::getSaveFileName(this,tr("项目另存为"),"",tr("GOWIN Project (*.gwprj)"));
    tmp=st->saveScenes().toHex();

    fi = QFileInfo(this->file_ProjectSave);
    name = fi.baseName();

    QFile file(this->file_ProjectSave); //相对路径、绝对路径、资源路径都可以
    if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以用QIODevice，Truncate表示清空原来的内容
        return;
    QXmlStreamWriter stream(&file);
    stream.setAutoFormatting(true);
    stream.writeStartDocument();
    stream.writeStartElement("SceneView");
    stream.writeAttribute("ScenePic", file_Background);
    stream.writeTextElement("SceneMarks", tmp);
    stream.writeEndElement();
    stream.writeEndDocument();
    file.close();

    //writeXML(this->file_ProjectSave);
    //st->saveScenes(file_ProjectSave);
}

void MainWindow::on_actionBackground_triggered()
{
    //QFileInfo fi;
    file_Background = QFileDialog::getOpenFileName(this,tr("打开背景图"),"","Image File(*.jpg)");
    //fi = QFileInfo(file_Background);
    //qDebug()<<file_Background;
    st->setScenePicFile(file_Background);
}



void MainWindow::on_actionLoad_triggered()
{
    QByteArray tmp;
    QXmlStreamReader reader;
    QString filename;
    QList<SceneMark *> scenelist;
    //ui->gridLayout_2->removeWidget(st);
    st->clearScenes();
    //delete st;

    filename=QFileDialog::getOpenFileName(this,tr("打开工程"),"",tr("GOWIN Project (*.gwprj)"));
    //qDebug()<<filename;

    QFile file(filename);
    file.open(QFile::ReadOnly);
    reader.setDevice(&file);
    while(!reader.atEnd()) {
        if(reader.isStartElement()) {
            if(reader.name() == "SceneView") {
                this->file_Background=reader.attributes().value("ScenePic").toString();
                while(!reader.atEnd())
                {
                    if(reader.name()=="SceneMarks")
                    {
                        tmp=QByteArray::fromHex(reader.readElementText().toLatin1());
                        reader.readNext();
                    }
                    reader.readNext();
                }
            }
            else reader.readNext();
        } else {
            reader.readNext();
        }
    }
    // 如果读取过程中出现错误，那么输出错误信息
    if (reader.hasError()) {
        qDebug() << "error: " << reader.errorString();
    }
    file.close();
    newpage();

    st->setScenePicFile(this->file_Background);
    qDebug()<<&tmp;
    st->loadScenes(tmp);
    scenelist=st->getScenes();
    foreach(SceneMark* index,scenelist)
    {
        index->setConfig();
    }

}

